package com.cskaoyan.market.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName MarketAuthenticator
 * @Description
 * @Author MZ
 * @Date 2023/5/25 9:05
 */
public class MarketAuthenticator extends ModularRealmAuthenticator {

    @Override
    protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
        //这里面便是我们的认证逻辑
        CskaoyanToken token = (CskaoyanToken) authenticationToken;
        String type = token.getType();
        Collection<Realm> realms = getRealms();
        List<Realm> realmList = new ArrayList<>();
        for (Realm realm : realms) {
            //realm的命名一定要命名成wxRealm或者adminRealm
            if(realm.getName().toLowerCase().contains(type)){
                //当前认证需要使用到的realm
                realmList.add(realm);
            }
        }
        //根据你的realm的数量，你是调用单个realm数据源获取数据还是调用多个数据源获取数据
        return realmList.size() == 1 ? doSingleRealmAuthentication(realmList.get(0), token) : doMultiRealmAuthentication(realmList, token);
    }

}
